package com.stormrage.edu.dao.edu.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.stormrage.edu.dao.edu.EduVideoDao;
import com.stormrage.entity.edu.EduVideo;
import com.stormrage.entity.edu.rep.EduVideoRep;
import com.stormragetech.core.dao.MysBaseDao;
import com.stormragetech.core.exception.StormrageTechException;

/**
 * @Title: EduVideoDaoImpl.java
 * @Package com.stormrage.edu.dao.edu.impl
 * @Description: 课件 Dao
 * @author www.stormragetech.com
 * @date 2016年4月8日
 * @version V1.0
 */
@Repository
public class EduVideoDaoImpl extends MysBaseDao implements EduVideoDao {

	private static final Logger logger = LoggerFactory.getLogger(EduVideoDaoImpl.class);

	@Override
	public Integer create(final EduVideo arg0) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 EduVideoDao 】-【 Create 】- 开始运行 ");

		final String query = "INSERT INTO edu_video (video, thumb, url, video_len) VALUES"
				+ " (:video, :thumb, :url, :videoLen)";
		logger.debug(" 数据库执行语句为 " + query);

		Integer index = create(query, arg0);

		logger.debug(" 【 Dao 】-【 EduVideoDao 】-【 Create 】- 完成运行 ");
		return index;
	}

	@Override
	public void update(final EduVideo arg0) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 EduVideoDao 】-【 Update 】- 开始运行 ");

		final String query = "UPDATE edu_video SET video = :video WHERE id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		update(query, arg0);

		logger.debug(" 【 Dao 】-【 EduVideoDao 】-【 Update 】- 完成运行 ");
	}

	@Override
	public void updateVideo(final EduVideo arg0) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 EduVideoDao 】-【 updateVideo 】- 开始运行 ");

		final String query = "UPDATE edu_video SET thumb = :thumb, url = :url, video_len = :videoLen WHERE id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		update(query, arg0);

		logger.debug(" 【 Dao 】-【 EduVideoDao 】-【 updateVideo 】- 完成运行 ");
	}

	@Override
	public void updateThumb(final EduVideo arg0) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 EduVideoDao 】-【 updateThumb 】- 开始运行 ");

		final String query = "UPDATE edu_video SET thumb = :thumb WHERE id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		update(query, arg0);

		logger.debug(" 【 Dao 】-【 EduVideoDao 】-【 updateThumb 】- 完成运行 ");
		
	}

	@Override
	public void delete(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EduVideoDao 】- 【 Delete 】- 开始运行 ");

		final String query = "DELETE FROM edu_video WHERE id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		update(query, map);

		logger.debug(" 【 Dao 】- 【 EduVideoDao 】- 【 Delete 】- 完成运行 ");
	}

	@Override
	public Integer queryCount(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EduVideoDao 】- 【 QueryCount 】- 开始运行 ");

		final String query = "SELECT COUNT(*) FROM edu_video " + map.get("query");
		logger.debug(" 数据库执行语句为 " + query);

		Integer count = queryCount(query, map);

		logger.debug(" 【 Dao 】- 【 EduVideoDao 】- 【 QueryCount 】- 完成运行 ");
		return count;
	}

	@Override
	public EduVideo queryById(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EduVideoDao 】- 【 QueryById 】- 开始运行 ");

		final String query = "SELECT * FROM edu_video WHERE id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		EduVideo entity = queryObject(query, EduVideo.class, map);

		logger.debug(" 【 Dao 】- 【 EduVideoDao 】- 【 QueryById 】- 完成运行 ");
		return entity;
	}

	@Override
	public List<EduVideo> queryList() throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EduVideoDao 】- 【 QueryList 】- 开始运行 ");

		final String query = "SELECT * FROM edu_video";
		logger.debug(" 数据库执行语句为 " + query);

		List<EduVideo> array = queryList(query, EduVideo.class);

		logger.debug(" 【 Dao 】- 【 EduVideoDao 】- 【 QueryList 】- 完成运行 ");
		return array;
	}

	@Override
	public List<EduVideo> queryList(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EduVideoDao 】- 【 QueryList 】- 开始运行 ");

		final String query = "SELECT * FROM edu_video" + map.get("query") + " LIMIT :startPage, :size";
		logger.debug(" 数据库执行语句为 " + query);

		List<EduVideo> array = queryList(query, EduVideo.class, map);

		logger.debug(" 【 Dao 】- 【 EduVideoDao 】- 【 QueryList 】- 完成运行 ");
		return array;
	}

	@Override
	public List<EduVideo> queryListById(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EduVideoDao 】- 【 QueryListById 】- 开始运行 ");

		final String query = "SELECT a.* FROM edu_video AS a, edu_chap_video AS b"
				+ " WHERE a.id = b.video_id AND b.chap_id = :id ORDER BY a.id";
		logger.debug(" 数据库执行语句为 " + query);

		List<EduVideo> array = queryList(query, EduVideo.class, map);

		logger.debug(" 【 Dao 】- 【 EduVideoDao 】- 【 QueryListById 】- 完成运行 ");
		return array;
	}

	@Override
	public List<EduVideo> queryListByMajorId(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EduVideoDao 】- 【 QueryListByMajorId 】- 开始运行 ");

		final String query = "SELECT a.* FROM edu_video AS a, edu_chap_video AS b, edu_course_chap AS c,"
				+ " edu_module_course AS d, edu_major_module AS e WHERE a.id = b.video_id AND b.chap_id = c.chap_id"
				+ " AND c.course_id = d.course_id AND d.module_id = e.module_id AND e.major_id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		List<EduVideo> array = queryList(query, EduVideo.class, map);

		logger.debug(" 【 Dao 】- 【 EduVideoDao 】- 【 QueryListByMajorId 】- 完成运行 ");
		return array;
	}

	@Override
	public List<EduVideoRep> queryRepListById(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EduVideoDao 】- 【 QueryRepListById 】- 开始运行 ");

		final String query = "SELECT a.id, a.video, a.video_len AS len, b.id AS viewId FROM edu_video AS a, edu_chap_video AS b"
				+ " WHERE a.id = b.video_id AND b.chap_id = :id ORDER BY a.id";
		logger.debug(" 数据库执行语句为 " + query);

		List<EduVideoRep> array = queryList(query, EduVideoRep.class, map);

		logger.debug(" 【 Dao 】- 【 EduVideoDao 】- 【 QueryRepListById 】- 完成运行 ");
		return array;
	}

}
